//创建一个路由对象并出
import Vue from 'vue'
import VueRouter from 'vue-router'
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)
const Home = () => import('../views/Home.vue')
const Photo = () => import('../views/Photo.vue')
const Album = () => import('../views/Album.vue')
const LoginRegister = () => import('../views/LoginRegister.vue')
const Upload = () => import('../views/Upload.vue')
const Theme = () => import('../views/Theme.vue')
const Person = () => import('../views/Person.vue')
const Recycle = () => import('../views/Recycle.vue')
const Moment = () => import('../views/Moment.vue')
const Piccut = () => import('../views/Piccut.vue')
const Search = () => import('../views/Search.vue')
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: LoginRegister,
    meta: { title: '智能相册-登陆' }

  },
  {
    path: '/home',
    component: Home,
    redirect: '/home/photo',
    meta: { title: 'Home' },
    children: [
      {
        path: 'photo',
        component: Photo,
        meta: { title: '智能相册-照片' }
      },
      {
        path: 'album',
        component: Album,
        meta: { title: '智能相册-自动分类' }
      },
      {
        path: 'theme',
        component: Theme,
        meta: { title: '智能相册-主题' }
      },
      {
        path: 'person',
        component: Person,
        meta: { title: '智能相册-人物' }
      },
      {
        path: 'upload',
        component: Upload,
        meta: { title: '智能相册-上传' }
      },
      {
        path: 'recycle',
        component: Recycle,
        meta: { title: '智能相册-回收站' }
      }, {
        path: 'moment',
        component: Moment,
        meta: { title: '智能相册-精彩时刻' }
      }, {
        path: 'piccut',
        component: Piccut,
        meta: { title: '编辑' }
      }, {
        path: 'search',
        component: Search,
        meta: { title: '搜索' }
      }

    ]
  },

]
let router = new VueRouter({
  routes: routes,
})

import store from '../store/index.js'
//导航前置守卫，from为当前路由,next()方法表示跳转「可进行逻辑判断，如验证权限」
router.beforeEach((to, from, next) => {
  // console.log(from)
  // console.log(to)
  store.commit("photo/clearChecked")

  let { title } = to.meta
  //console.log('to  ' + title)
  document.title = title
  //console.log('是否验证' + store.state.login.isAuthenticated)

  const isAuth = window.sessionStorage.getItem('isAuthenticated')
  const token = window.sessionStorage.getItem('token')
  store.dispatch("login/storeToken", token);
  if (to.path !== '/login' && !isAuth) next({ path: '/login' })
  else next()
})
//$router 路由实例
//$route 当前路由信息
export default router